package com.hsy.swu.other.game2048

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hsy.swu.databinding.ItemGame2048RankBinding
import com.hsy.swu.hsyutils.MActivityUtil
import com.hsy.swu.hsyutils.setFont

class RankAdapter : RecyclerView.Adapter<RankAdapter.RankViewHolder>() {
    var mRankList = emptyList<RankItem>()
        @SuppressLint("NotifyDataSetChanged")
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RankViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        return RankViewHolder(ItemGame2048RankBinding.inflate(layoutInflater, parent, false))
    }

    override fun getItemCount(): Int {
        return mRankList.size
    }

    override fun onBindViewHolder(holder: RankViewHolder, position: Int) {
        holder.bind(mRankList[position])
    }

    class RankViewHolder(private val mBinding: ItemGame2048RankBinding) :
        RecyclerView.ViewHolder(mBinding.root) {
        fun bind(rankItem: RankItem) {
            mBinding.apply {
                tvRank.visibility = View.VISIBLE
                img.visibility = View.GONE

                tvRank.textSize = when (rankItem.rank) {
                    "1" -> {
                        tvRank.visibility = View.GONE
                        img.visibility = View.VISIBLE
                        48f
                    }

                    "2" -> 32f
                    "3" -> 24f
                    else -> 16f
                }
                tvRank.text = rankItem.rank
                tvName.text = rankItem.info
                tvPBScore.text = "${rankItem.pbScore}"
                setFont()
                mBinding.root.animate().scaleX(0.8f).scaleY(0.8f).alpha(0f).setDuration(0).start()
                MActivityUtil.scaleAnimOnShow(mBinding.root)
            }
        }
    }
}